// pages/healthManage/manageDetail/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    item:{},
    itemtwo:{},
    serialOrLotNumber:"",
    description:"",
    workOrderCode:"",
    planTime:"",
    workOrderType:"",
    plan:"",
    endTime:"",
    engineer:"",
    statusName:"",
    status:"",
    surdelete:false,
    content:"",
    id:"",
    iosDialog1:false,
    dataList:[],
    allData:[],
    resultjuge:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  close: function () {
    this.setData({
        iosDialog1: false,
    })
},
closesurdelete(){
  this.setData({
    surdelete: false
})
},
deleteManage(){
  this.setData({
    surdelete: true
})
},
sureDelete(){
  let self = this
  self.axios.remove("HealthControlInformation", this.data.id)
  .then(()=> {
    this.setData({
      surdelete: false
  })
  wx.showToast({
    title: '删除成功',
    icon: 'success',
    duration: 2000,
    success: () => {
      self.Router.push({
        path: "",
        query: {},
        openType: 'back',
        duration: 2000,
      })
    }
  })
 
  })
  .catch(e=>{
    self.$notify(e.data.msg, "error");
  })
},
sureData(){
  let self = this;
  let obj = { list: self.data.allData, informationId: self.data.id, type: 1 };
  let param = { code: self.data.serialOrLotNumber, workOrderCode: self.data.workOrderCode };
  self
  .axios
  .add("HealthControlExecute", obj, false)
  .then((res) => {
    if(res.code==200){
      wx.showToast({
        title: '保存成功',
        icon: 'succes',
        duration: 1000,
        mask:true
    })
    this.setData({
      iosDialog1: false,
  })
      wx.navigateTo({
        url: '../report/index',
            success:function(res){
             res.eventChannel.emit('toreport',param)
      }
     })
    }

  })
  .catch((e) => {

  });
},
  report() {
    let self = this;
    self
    .axios
    .query("HealthControlInformation", {}, { id: self.data.item.id }, "", false)
    .then((res) => {
      console.log(res)
      if(res.data.rows[0].resultList){
        res.data.rows[0].resultList.forEach(e=>{
          e.result = e.resultRecord
        })
        self.setData({
          allData:res.data.rows[0].resultList,
          resultjuge:true
        })
      }else{
        self.setData({
          resultjuge:false
        })
      }
      if(!self.data.resultjuge){
        wx.showToast({
          title: '请先填写执行计划',
          icon: 'error',
          duration: 1000,
          mask:true
      })
      return
      }
      let param = { code: self.data.serialOrLotNumber, workOrderCode: self.data.workOrderCode,list: self.data.allData,informationId: self.data.id,type:1};
      wx.navigateTo({
        url: '../report/index',
            success:function(res){
             res.eventChannel.emit('toreport',param)
      }
     })
      
      // let obj = { list: self.data.allData, informationId: self.data.id, type: 1 };
      // console.log(self.data.serialOrLotNumber)
      // let checkObj={list: self.data.allData,code: self.data.serialOrLotNumber, workOrderCode: self.data.workOrderCode,type:1,informationId: self.data.id}
      // self
      //   .axios
      //   .custom("HealthControlRecordCheck", "checkRecord", checkObj)
      //   .then(function (response) {
      //     if(response.code==200){
      //       if(response.data.isOut==0){
      //         self
      //           .axios
      //           .add("HealthControlExecute", obj, false)
      //           .then((res) => {
      //             if(res.code==200){
      //               wx.showToast({
      //                 title: '保存成功',
      //                 icon: 'succes',
      //                 duration: 1000,
      //                 mask:true
      //             })
      //               wx.navigateTo({
      //                 url: '../report/index',
      //                     success:function(res){
      //                      res.eventChannel.emit('toreport',param)
      //               }
      //              })
      //             }
  
      //           })
      //           .catch((e) => {
                  
      //           });
      //      }else{
      //        let content=""
      //        content=response.data.content.split(";")
      //        content.pop()
      //        console.log(content)
      //        self.setData({
      //          iosDialog1:true,
      //          content:content
      //        })
      //      }
      //     }
      //     console.log(response)
  
      //   });


    }).catch(()=>{

    })
    
  
  },
toEdtionManage(){
    let self = this
        wx.navigateTo({
          url: '../edtionManage/index',
              success:function(res){
               res.eventChannel.emit('headlEd',{item:self.data.itemtwo,statusName:self.data.statusName})
        }
      })
      },
  checkreport(){
    let self =this
    let param = { code: self.data.serialOrLotNumber, workOrderCode: self.data.workOrderCode,statusName:self.data.statusName };
    wx.navigateTo({
      url: '../report/index',
          success:function(res){
           res.eventChannel.emit('toreport',param)
    }
   })
  },
getAlldata(){
  let self = this;
  self
    .axios
    .query("HealthControlInformation", {}, { id: self.data.item.id }, "", false)
    .then((res) => {
      console.log(res)
      self.setData({
        itemtwo:res.data.rows[0]
      })
      if(res.data.rows[0].resultList){
        console.log("here1")
        res.data.rows[0].resultList.forEach(e=>{
          e.result = e.resultRecord
        })
        self.setData({
          allData:res.data.rows[0].resultList,
          resultjuge:true
        })
      }else{
        self.setData({
          resultjuge:false
        })
      }
      console.log("here")
      self.setData({
        serialOrLotNumber:res.data.rows[0].code,
        description:res.data.rows[0].description,
        workOrderCode:res.data.rows[0].workOrderCode,
        planTime:res.data.rows[0].planTime.substring(0,res.data.rows[0].planTime.length-8),
        workOrderType:res.data.rows[0].workOrderType,
        plan:res.data.rows[0].plan,
        endTime:res.data.rows[0].endTime.substring(0,res.data.rows[0].endTime.length-8),
        engineer:res.data.rows[0].engineer,
        status:res.data.rows[0].status,
        id:res.data.rows[0].id,
        dataList:res.data.rows[0].resultList
      })
    }).catch(()=>{

    })
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  dealManage(e){
    console.log(e)
    let self = this
    wx.navigateTo({
      url: '../dealManage/index',
          success:function(res){
           res.eventChannel.emit('toDeal',{code:self.data.serialOrLotNumber,id:self.data.id,workOrderCode:self.data.workOrderCode})
    }
  })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let self = this
    const eventChannel = this.getOpenerEventChannel();
		// 监听 index页面定义的toHome 事件
		eventChannel.on('tomanageD', (res) => {
      console.log(res,'页面传值') 
      this.setData({
        item:res.item,
        statusName:res.item.statusName
      })
      self.getAlldata()
		})
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})